//插件轮播图 -->
var swiper = new Swiper('.swiper-container', {
    pagination: '.swiper-pagination',
    nextButton: '.swiper-button-next',
    prevButton: '.swiper-button-prev',
    paginationClickable: true,
    spaceBetween: 30,
    centeredSlides: true,
    autoplay: 2500,
    autoplayDisableOnInteraction: false
});
// 显示注册页面
$(".zhuce1").on("click",function(){
    $(".zhuce,#zz").show();
})
//隐藏注册页面
$(".close").on("click",function(){
    $(".zhuce,#zz").hide();
    $('.r_username').val('');
    $('.r_password').val('');
    $('.r_name').val('');
    $('.r_sex').val('');
    $('.r_telephone').val('');
    $('.r_email').val('');
});
// 显示登录页面
$(".login1").on("click",function(){
    $(".login,#zz").show();
})
//隐藏登录页面
$(".l_close").on("click",function(){
    $(".login,#zz").hide();
    $('.l_username').val('');
    $('.l_password').val('');
});
// 显示发布页面
$(".release1").on("click",function(){
    $(".release,#zz").show();
})
//隐藏发布页面
$(".r_close").on("click",function(){
    $(".release,#zz").hide();
});
var imgList = [];
var imgurl  = '';
//上传附件
function uploadImg() {
    // debugger;
    imgurl = '';
    if($("#file").val() != "") {
        var form = document.getElementById("uploadF");
		var formData = new FormData(form);
		// formData.append('testName', '我是传送文件标识')
        $.ajax({
            type: "POST",
            url:upUrl + "/uploadImg",//后台接口
            data: formData,
			processData: false,//告诉jq不要处理发送的数据
			contentType: false,//告诉jq不要设置content-Type请求头
            success: function(d){
                if(d.code == 200) {
                    imgList.push(d.message)
                    imgurl = d.message
                    //图片显示
                    $("#avatar").attr("value",d.message);
                    $("#avatarShow").attr("src",d.message);
                }
            },
        });
    } else {
        toastr.error('请先选择文件');
    }
}

var pageNum = 1;
var route = '';
var pageNum = 1;
var total = 0;
$(function(){
    getDataList();
    $('.input-text').html('');
    console.log($('.input-text').val());
    // if (window.sessionStorage.getItem('admin') == null) {
    //     $(".admin").hide();
    //     $(".login1").show();
    // } else {
    //     $(".admin").text(JSON.parse(window.sessionStorage.getItem('admin')).name).show();
    //     $(".login1").hide();
    // }
    // window.localStorage.getItem('admin')
});
//获取数据
function getDataList (categoryName) {
    console.log("传过来的值：",categoryName)
    $.ajax({
        type:"get",
        url: upUrl + "/dish/front/dishList",
        dataType: "json",
        data: {
            name: route,
            categoryName:categoryName,
            pageNum: pageNum,
            pageSize: 8,
        },
        success:function(data){
            var testList = data.result.records;
            total = data.result.total;
            console.log(total)
            var orderBody = '';
            $.each(testList, function (index, val) {
                // <span class="less">-</span>\
                // <span class="num">0</span>\
                // <span class="puls">+</span>\
                orderBody += '<div class="t_item">\
                    <div class="t_item_top">\
                        <a href="page/placeOrder.html" class="fl second-nav-cell">\
                            <img src="' + val.rimage + '" alt="">\
                        </a>\
                    </div>\
                    <div class="item_text" :title="' + val.name + '">\
                        ' + val.name +'|'+val.categoryName+ '\
                    </div>\
                    <div class="item_mony">\
                        <div class="left_m"> ￥' + val.price + '</div>\
                        <div class="right_d">\
                            <span class="add_puls" onclick="addShopping(' + val.id + ')">下单</span>\
                        </div>\
                    </div>\
                </div>'
            })
            $('.t_flex').html(orderBody);
            new Pagination({
                element: '#pages_1',
                type: 2,
                pageIndex: pageNum,
                pageSize: 8,
                pageCount: 9,
                total: total,
                jumper: true,
                singlePageHide: false,
                disabled: true,
                currentChange: function(index) {
                    console.log(index);
                    pageNum = index;
                    getDataList(categoryName);
                }
            });
        }
    })
}
// 加入购物车事件
function addShopping (val) {
    // 请求接口传递参数
    $.ajax({
        type:"post",
        url: upUrl + "/cart/addCart",
        data: {
            dishId: val
        },
        success:function(data){
            if (data.code == 200) {
                toastr.success("添加下单商品成功!");
                getUserList();
            } else {
                if(data.msg){

                }
                toastr.error('添加下单商品失败');
            }
        },
        error:function (error) {
            toastr.error('添加下单商品失败');
        }
    })
};
//  搜索事件
function searchClick (categoryName) {
    console.log("点一下的值：",categoryName)
    route = $('.input-text').val();
    console.log($('.input-text').val(),"1111")
    getDataList(categoryName);
};
/*-------------------------------登录逻辑开始-----------------------------------------*/
// 登录按钮事件
function loginClick () {
    if ($('.l_phone').val() =='') {
        toastr.warning("请输入账号!");
        return;
    } else if ($('.l_password').val() =='') {
        toastr.warning("请输入密码!");
        return;
    } else {
        // 请求接口传递参数
        $.ajax({
            type:"get",
            url: upUrl + "/loginUser",
            data: {
                username: $('.l_phone').val(),
                password: $('.l_password').val()
            },
            success:function(data){
                if (data.code == 200) {
                    toastr.success("登录成功!");
                    $(".login,#zz").hide();
                    $('.l_username').val('');
                    $('.l_password').val('');
                    //用户信息存储起来
                    sessionStorage.setItem('admin', JSON.stringify(data.result))
                    $(".admin").text(data.result.name).show();
                    $(".login1").hide();
                    getUserList();
                } else {
                    toastr.error('用户名或密码错误');
                }
            },
            error:function (error) {
                // $(".admin").show();
                // $(".login1").hide();
                // window.localStorage.setItem('admin', 'admin')
            }
        })
    }
};
// 退出功能
function lout () {
    $.ajax({
        type:"post",
        url: upUrl + "/exit",
        data: {},
        dataType: "json",
        success:function(data){
            if (data.code == 200) {
                toastr.success("退出成功!");
                // 重新刷新表格数据
                // ./login.html
                window.sessionStorage.clear()
            } else {
                toastr.error(data.message);
            }
        }
    })

};




// 获取用户信息
function getUserList() {
    $.ajax({
        type:"get",
        url: upUrl + "/session",
        data: {},
        success:function(data){
            if (data.code == 200) {
                console.log(data)
                //用户信息存储起来
                window.sessionStorage.setItem('admin', JSON.stringify(data.result))
                $(".admin").text(data.name).show();
                $(".login1").hide();
            } else {
                toastr.error(data.message);
            }
        },
    })

}

/*-------------------------------登录逻辑结束-----------------------------------------*/
/*-------------------------------注册逻辑开始-----------------------------------------*/
function registered () {
    if ($('.r_username').val() =='') {
        toastr.warning("请输入用户名!");
        return;
    } else if ($('.r_password').val() =='') {
        toastr.warning("请输入密码!");
        return;
    } else if ($('.r_name').val() =='') {
        toastr.warning("请输入真实姓名!");
        return;
    } else if ($('.r_sex').val() =='') {
        toastr.warning("请输入性别!");
        return;
    } else if ($('.r_telephone').val() =='') {
        toastr.warning("请输入手机!");
        return;
    } else if ($('.r_email').val() =='') {
        toastr.warning("请输入邮箱!");
        return;
    } else {
        // 请求接口传递参数
        $.ajax({
            type:"get",
            url: upUrl + "/registerUser",
            data: {
                username: $('.r_username').val(),
                password: $('.r_password').val(),
                name: $('.r_name').val(),
                sex: $('input[name="sex"]:checked').val(),
                phone: $('.r_telephone').val(),
                email: $('.r_email').val(),
            },
            success:function(data){
                if (data.code == 200) {
                    toastr.success("注册成功!");
                    $(".zhuce,#zz").hide();
                    $('.r_username').val('');
                    $('.r_password').val('');
                    $('.r_name').val('');
                    $('.r_sex').val('');
                    $('.r_telephone').val('');
                    $('.r_email').val('');
                } else {
                    toastr.error(data.message);
                }
            }
        })
    }
}
/*-------------------------------注册逻辑结束-----------------------------------------*/
/*-------------------------------购物车逻辑-----------------------------------------*/
// 菜单加号事件
function pulsClick () {
    num = $('.num').html();
    console.log(num)
    num = parseInt(num);
    if (num >= 0) {
        num++;
    }
    console.log(num)
    $('.num').html(num)
}
// 菜单减号事件
function lessClick () {
    num = $('.num').html();
    console.log(num)
    num = parseInt(num);
    if (num > 0) {
        num--;
    }
    console.log(num)
    $('.num').html(num)
}
/*-------------------------------发布日志逻辑开始-----------------------------------------*/
function release () {
    console.log($('#file').val())
    console.log("tup 路径："+imgurl);

    if ($('.textarea').val() =='') {
        toastr.warning("请输入旅游心得");
        return;
    } else if ($('#file').val() =='') {
        toastr.warning("请输入上传图片");
        return;
    } else {
        // 请求接口传递参数
        $.ajax({
            type:"post",
            url: upUrl + "/journal/addJournal",
            data: {
                content: $('.textarea').val(),
                title: '',
                imgurl: imgurl,
            },
            success:function(data){
                toastr.success("日志发布成功!");
                $(".release,#zz").hide();
            }
        })
    }
}
/*-------------------------------发布日志逻辑结束-----------------------------------------*/
/*----------------------------------------------------图片上传---------------------------------------------*/
$(".fileUpload").change(function () {
    //获取选择图片的对象
    var docObj = $(this)[0];
    var picDiv = $(this).parents(".fileImgDiv");
    var fileList = docObj.files;
    for (var i = 0; i < fileList.length; i++) {
        //动态添加html元素
        var picHtml = "<div class='imageDiv' > <img style='object-fit:cover;' id='img" + fileList[i].name + "' /> <div class='cover'><i class='delbtn'>删除</i></div></div>";
        picDiv.prepend(picHtml);
        //获取图片imgi的对象
        var imgObjPreview = document.getElementById("img" + fileList[i].name);
        if (fileList && fileList[i]) {
            //图片属性
            imgObjPreview.style.display = 'block';
            imgObjPreview.style.width = '100px';
            imgObjPreview.style.height = '100px';
            if (userAgent.indexOf('MSIE') == -1) {
                //IE以外浏览器
                imgObjPreview.src = window.URL.createObjectURL(docObj.files[i]); //获取上传图片文件的物理路径;
            } else {
                //IE浏览器
                if (docObj.value.indexOf(",") != -1) {
                    var srcArr = docObj.value.split(",");
                    imgObjPreview.src = srcArr[i];
                }
                else { imgObjPreview.src = docObj.value; }
            }
        }
    }
    /*删除图片功能*/
    $(".delbtn").click(function () {
        var _this = $(this);
        _this.parents(".imageDiv").remove();
    });
});